</div>
</div>
<div class="footer">
	<div class="copy">
		<span>
			&copy;MXCRM&2018
		</span>
	</div>
</div>
<div class="modal inmodal fade" id="Modal1" tabindex="-1"  style=" overflow:auto; border:1px solid #000000;" role="dialog" >
    <div class="modal-dialog modal-md" style="width:700px;">
        <div class="modal-content" id="login_modal">

        </div>
    </div>
</div>
<script>

//购物车
function Cart () {
  this.Count = 0;
  this.Total = 0;
  this.Items = new Array();
};
//购物车集合对象
function CartItem() {
  this.Id = 0;
  this.Name = "";
  this.Count = 0;
  this.Price = 0;
  this.Subtotal = 0;
};
var cookieName = "yxhCart";
function Add (id, name, count, price, subtotal,sketch,img) {
    var cart = Read();
    var index = Find(id);
    //如果ID已存在，覆盖数量
    if (index > -1) {
      cart.Total -= (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
      cart.Items[index].Count = count;
      cart.Items[index].Subtotal = subtotal;
      cart.Items[index].Sketch = sketch;
      cart.Items[index].Img = img;
      cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
    } else {
      var item = new CartItem();
      item.Id = id;
      item.Name = name;
      item.Count = count;
      item.Price = price;
      item.Subtotal = subtotal;
      item.Sketch = sketch;
      item.Img = img;
      cart.Items.push(item);
      cart.Count++;
      index++;//error
      cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);
    }
    Save(cart);
    return cart;
};
//根据ID查找
function Find (id) {
	var cart = Read();
	var index = -1;
	// var index = cart.Items.length+1;
	for (var i = 0; i < cart.Items.length; i++) {
	  if (cart.Items[i].Id == id) {
	    index = i;
	  }
	}
	return index;
};
//COOKIE操作
function Save (cart) {
	var source = "";
	for (var i = 0; i < cart.Items.length; i++) {
	  if (source != "") { source += "|$|"; }
	  source += ItemToString(cart.Items[i]);
	}
	$.cookie(cookieName, source);
};
function Read () {
    // 读取COOKIE中的集合
    var source = $.cookie(cookieName);
    var cart = new Cart();
    if (source == null || source == "") {
      return cart;
    }
    var arr = source.split("|$|");
    cart.Count = arr.length;
    for (var i = 0; i < arr.length; i++) {
      var item = ItemToObject(arr[i]);
      cart.Items.push(item);
      cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000);
    }
    return cart;
};
//改变数量
function Change (id, count) {
	var cart = Read();
	var index = Find(id);
	cart.Items[index].Count = count;
	Save(cart);
	return cart;
};
//移出购物车
function Del (id) {
	var cart = Read();
	var index = Find(id);
	if (index > -1) {
	  var item = cart.Items[index];
	  cart.Count--;
	  cart.Total = cart.Total - (((item.Count * 100) * (item.Price * 100)) / 10000);
	  cart.Items.splice(index, 1);
	  Save(cart);
	}
	return cart;
};
// function removeCookie() { 
// 	if(document.cookie != '' && confirm('你想清理所有cookie吗？')) { 
// 		var arrCookie = document.cookie.split('; '); 
// 		var arrLength = arrCookie.length; 
// 		var expireDate = new Date(); 
// 		expireDate.setDate(expireDate.getDate()-1); 
// 		for(var i=0; i<arrLength; i++) { 
// 		var str = arrCookie[i].split('=')[0]; 
// 		document.cookie = str+ '=' + ';expires=' + expireDate.toGMTString(); 
// 		} 
// 	}
// }

function ItemToString (item) {
    return item.Id + "||" + escape(item.Name) + "||" + item.Count + "||" + item.Price + "||" + item.Subtotal + "||" + item.Sketch + "||" + item.Img;
};
function ItemToObject (str) {
	var arr = str.split('||');
	var item = new CartItem();
	item.Id = arr[0];
	item.Name = unescape(arr[1]);
	item.Count = arr[2];
	item.Price = arr[3];
	item.Subtotal = arr[4];
	item.Sketch = arr[5];
	item.Img = arr[6];
	return item;
};
</script>
</body>
</html>